#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>



#include "lib.h"


char *urlEncode(const char *src, char *dest, int size)
{
    const unsigned char *s = (const unsigned char *) src;
    char *d = dest;
    char *end = dest + size - 1;
    while (*s != '\0' && d != end) {
        if (isalnum(*s) || *s == '.') {
            *d ++ = *s ++;
        } else if (*s == ' ') {
            *d ++ = '+';
            s ++;
        } else {
            int len = (int) (end - d);
            if (len < 3) {
                break;
            }
            snprintf(d, len + 1, "%%%02x", *s);
            d += 3;
            s ++;
        }
    }
    *d = '\0';
    return dest;
}

static char get_time_buf[128]; 
char * _get_time_string() {
    struct tm *newtime; 
    time_t localTime1; 
    time( &localTime1 ); 
    newtime=localtime(&localTime1);

    memset(get_time_buf, 0, sizeof(get_time_buf));
    strftime(get_time_buf, 128, "%F %T", newtime); 
    return get_time_buf;
}

static char get_scoends_buf[128];
static unsigned long last_seconds = 0;
char *_get_time_num() {
    struct timeval now;
    gettimeofday(&now, NULL);
    memset(get_scoends_buf, 0, sizeof(get_scoends_buf));
    snprintf(get_scoends_buf, sizeof(get_scoends_buf), "%lu %lu %lu",
        now.tv_sec- last_seconds, now.tv_usec?now.tv_usec/1000:0, now.tv_usec?now.tv_usec%1000:0);
    if(last_seconds ==0) {
        last_seconds = now.tv_sec;
    }
    return get_scoends_buf;
}

char *get_time_string2(){
    struct timeval lctime;
    time_t now;
    struct tm *timeinfo;
    char timestr[80];

    gettimeofday(&lctime, NULL);
    time(&now);
    timeinfo = gmtime(&now);
    strftime(get_scoends_buf, sizeof(get_scoends_buf),"%b %d %X",timeinfo);

    return get_scoends_buf;
}

